

#========================install mysql========================================================================                                              

# [方法一, 自定义安装路径]

# step 1 设置系统swap 分区大小，参考如下公式：
# RAM                    /     Swap Space
# Between 1 GB and 2 GB  /     1.5 times the size of the RAM
# Between 2 GB and 16 GB /     Equal to the size of the RAM
# More than 16 GB        /     16 GB
# 执行如下命令
# 然后，检查和设置swap那一行是否有被注释，如果被注释就要开启
cat /etc/fstab
# 查看swap 空间大小(总计)：
free -m
# 查看swap 空间(file(s)/partition(s))：
swapon -s


# 查看磁盘路径的空间
df -h /home


# 关闭所有的swap空间
swapoff -a


# 创建新的swap文件，bs=表示每个block分块大小是1024 byte，count表示多少个block分块，所以总大小是bs*count=4GB
dd if=/dev/zero of=/home/system-swap bs=1024 count=4194304
# 输出如下
# 4194304+0 records in
# 4194304+0 records out
# 4294967296 bytes (4.3 GB) copied, 29.991 s, 143 MB/s


# 设置这个分区的权限为600
chmod -R 644 /home/system-swap
 
 
# 把这个新建分区，变成swap分区
/sbin/mkswap /home/system-swap
# 输出如下
# Setting up swapspace version 1, size = 4194300 KiB
# no label, UUID=941e36a8-d389-4400-ad7d-07387e1da776


# 把这个新建分区，设置状态为open。
# 备注：重启之后，该swap分区还是失效，只有执行下面配置后才会永久生效。
/sbin/swapon /home/system-swap


# 设置重启后，swap分区仍然有效
# 编辑如下文件，修改swap行内容为新加分区/home/system-swap
cat /etc/fstab
##### /dev/mapper/centos-swap swap                    swap    defaults        0 0
# /home/system-swap  swap                    swap    defaults        0 0


# 关闭SELINUX，设置参数SELINUXTYPE=disabled
vi /etc/selinux/config
# 修改参数如下
# # SELINUXTYPE=targeted
SELINUXTYPE=disabled

# =============================================================================================================

# step 2: 安装系统依赖软件
# 修改yum为国内镜像 === 看具体情况，有时候国内镜像不一定完整，这个时候还是要切换回国外地址
# cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup-linux && \
# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \
yum clean all && \
yum makecache

# 清理掉无用的repo
yum --enablerepo=base clean metadata


# 安装依赖软件
yum install binutils -y && \
yum install compat-libstdc++-33 -y && \
yum install gcc -y && \
yum install gcc-c++ -y && \
yum install glibc -y && \
yum install glibc-devel -y && \
yum install libgcc -y && \
yum install libstdc++ -y && \
yum install libstdc++-devel -y && \
yum install libaio -y && \
yum install libaio-devel -y && \
yum install libXext -y && \
yum install libXtst -y && \
yum install libX11 -y && \
yum install libXau -y && \
yum install libxcb -y && \
yum install libXi -y && \
yum install make -y && \
yum install sysstat -y && \
yum install zlib-devel -y && \
yum install elfutils-libelf-devel -y


# yum 
rpm -q --queryformat %-{name}-%{version}-%{release}-%{arch}"\n" \ compat-libstdc++-33 glibc-kernheaders glibc-headers libaio libgcc glibc-devel xorg-x11-deprecated-libs
# 输出无法下载和安装的软件，清单如下：
# package  compat-libstdc++-33 is not installed
# package glibc-kernheaders is not installed
# package glibc-headers is not installed
# libaio-0.3.109-13.el7-x86_64
# libgcc-4.8.5-16.el7-x86_64
# package glibc-devel is not installed
# package xorg-x11-deprecated-libs is not installed

# 遇到部分无法在aliyun下载的软件，则需要重新替换yum.repo
# cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup-aliyun && \
# cp /etc/yum.repos.d/CentOS-Base.repo.backup-linux /etc/yum.repos.d/CentOS-Base.repo && \
yum clean all && \
yum makecache && \
yum install -y compat-libstdc++*  && \
yum install -y glibc-kernheaders*  && \
yum install -y glibc-headers*  && \
yum install -y libaio-*  && \
yum install -y libgcc-*  && \
yum install -y glibc-devel*  && \
yum install -y xorg-x11-deprecated-libs*  && \

  
# 确保，已经包含了libaio-0.3.106，默认开启异步I/O。
# 检查在操作系统中，是否开启AIO 异步读写IO
cat /proc/slabinfo | grep kio
# 如果没有开启，则在下面文件中，增加如下两行
vi /proc/slabinfo
kioctx     51    120    320   12    1 : tunables   54   27    8 : slabdata    10    10    0
kiocb      30     30    256   15    1 : tunables  120   60    8 : slabdata    2      2    0


# =============================================================================================================

# step 3: 创建mysql安装目录
mkdir -p /opt/soft/{jdk,mysql}
# 然后上传jdk或mysql 二进制文件到上面创建的软件目录

# 创建mysql的安装主目录
mkdir -p /app/mysql/mysql-8.0.18/conf && \
# 创建mysql的数据主目录
mkdir -p /data/mysql/mysql-8.0.18/ && \
# 创建mysql的日志主目录
mkdir -p /log/mysql/mysql-8.0.18/


# =============================================================================================================

# step 4: mysql安装用户和组的创建
# 使用root用户，进行如下操作：
# 创建ops_install组
groupadd  -g 5000 ops_install

# 创建ops_admin组
groupadd  -g 501 ops_admin

# 创建mysql用户
useradd -g ops_install  -G  ops_admin  mysql

# 修改mysql密码
echo 'password'|passwd --stdin mysql

# 删除用户和其以来的用户文件
# userdel -r mysql
# 查看用户mysql权限是否设置正确，正确输出结果如下
# id mysql
# [root@CNT7XKFKAD01 ~]# id mysql
# uid=1001(mysql) gid=5000(ops_install) groups=5000(ops_install),501(ops_admin)

# =============================================================================================================

# step 5: 安装用户的profile文件的设置

# 编辑/etc/profile，加入以下内容
vi /etc/profile
# -----------------------java env-----------------------------------------------------------------
JAVA_HOME=/env/jdk/jdk-12.0.2

PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
# -----------------------java env-----------------------------------------------------------------

# -----------------------mysql env---------------------------------------------------------------
MYSQL_BASEDIR=/app/mysql/mysql-8.0.18
MYSQL_DATADIR=/data/mysql/mysql-8.0.18
MYSQL_LOGDIR=/log/mysql/mysql-8.0.18
# MYSQL_REDOLOGDIR=/log/mysql/mysql-8.0.18/relaylog
# MYSQL_REDOIDXDIR=/log/mysql/mysql-8.0.18/relayidx
# MYSQL_BINLOGDIR=/log/mysql/mysql-8.0.18/binlog
MYSQL_CONF=/app/mysql/mysql-8.0.18/conf

PATH=$PATH:$MYSQL_BASEDIR/bin:$MYSQL_BASEDIR/lib
# -----------------------mysql env---------------------------------------------------------------


# 生效配置环境变量
source /etc/profile

# 检查生效环境变量
env | grep mysql
env | grep JAVA

# =============================================================================================================

# step 5: 安装jdk 8 
# 创建jdk的软件目录和安装目录，分别如下：
mkdir -p /opt/soft/jdk/ && \
mkdir -p /env/jdk/

# 然后，通过WinSCP工具，把JDK 8二进制安装包tar复制到软件目录
ls -al /opt/soft/jdk/jdk-12.0.2_linux-x64_bin.tar.gz

# 解压jdk到安装目录
tar -zxvf /opt/soft/jdk/jdk-12.0.2_linux-x64_bin.tar.gz -C /env/jdk/


# =============================================================================================================

# step 6: 安装MYSQL

# 创建相应的文件系统（或安装目录）
mkdir -p ${MYSQL_BASEDIR} && \
mkdir -p ${MYSQL_DATADIR} && \
mkdir -p ${MYSQL_LOGDIR} && \
mkdir -p ${MYSQL_CONF} && \
mkdir -p ${MYSQL_LOGDIR}/{error,pid,bin,relayidx,relaylog,slow}/ && \
# -zxf, 不显示解压进度信息
tar -zxvf /opt/soft/mysql/mysql-cluster-8.0.18-rc-linux-glibc2.12-x86_64.tar.gz -C ${MYSQL_BASEDIR} && \
mv ${MYSQL_BASEDIR}/mysql-cluster-8.0.18-rc-linux-glibc2.12-x86_64/* ${MYSQL_BASEDIR}/ && \
touch ${MYSQL_LOGDIR}/error/error.log && \
touch ${MYSQL_LOGDIR}/pid/pid.log && \
touch ${MYSQL_LOGDIR}/bin/bin.log && \
touch ${MYSQL_LOGDIR}/relaylog/relay.log && \
touch ${MYSQL_LOGDIR}/relayidx/relay.index && \
touch ${MYSQL_LOGDIR}/slow/slow.log


# 制作mysql的配置文件
# COPY soft/mysql.conf  /opt/install/mysql/conf/
# COPY soft/mysql.sh /app/install/mysql/
cat > ${MYSQL_CONF}/mysql.conf <<EOF
[mysqld]
port=13306
basedir=${MYSQL_BASEDIR}
datadir=${MYSQL_DATADIR}
# socket=${MYSQL_BASEDIR}/mysql.sock
pid-file=${MYSQL_LOGDIR}/pid/pid.log
log-error=${MYSQL_LOGDIR}/error/error.log
lower_case_table_names=1

slow_query_log_file=${MYSQL_LOGDIR}/slow/slow.log
relay-log=${MYSQL_LOGDIR}/relaylog/relay.log
relay-log-index=${MYSQL_LOGDIR}/relayidx/relay.index

gtid_mode=ON
enforce_gtid_consistency=ON
event_scheduler=1

character-set-server=utf8
init_connect='SET NAMES utf8'
max_connections=10000

innodb_log_file_size = 256M
# innodb_file_format=barracuda
innodb_strict_mode=0
innodb_file_per_table=on

log-bin=${MYSQL_LOGDIR}/bin/bin.log
binlog_format=row
sync_binlog=1

# skip-grant-tables]
EOF

# =============================================================================================================

# step 10: 配置mysql的文件访问权限 和 内存参数

# 授予mysql用户访问文件夹和文件的权限
chmod -R 744  ${MYSQL_BASEDIR} && \
chmod -R 744  ${MYSQL_DATADIR} && \
chmod -R 744  ${MYSQL_LOGDIR} && \
## chmod -R 744  ${MYSQL_CONF} && \
chmod -R 644  ${MYSQL_CONF}/mysql.conf && \
chmod -R 644  ${MYSQL_LOGDIR}/error/error.log && \
chmod -R 644  ${MYSQL_LOGDIR}/pid/pid.log && \
chown -R mysql:ops_install ${MYSQL_BASEDIR} && \
chown -R mysql:ops_install ${MYSQL_DATADIR} && \
chown -R mysql:ops_install ${MYSQL_LOGDIR} && \
## chown -R mysql:ops_install ${MYSQL_CONF} && \
chown -R mysql:ops_install ${MYSQL_CONF}/mysql.conf && \
chown -R mysql:ops_install ${MYSQL_LOGDIR}/error/error.log && \
chown -R mysql:ops_install ${MYSQL_LOGDIR}/pid/pid.log && \
chown -R mysql:ops_install ${MYSQL_LOGDIR}/relaylog/relay.log && \
chown -R mysql:ops_install ${MYSQL_LOGDIR}/relayidx/relay.index && \
chown -R mysql:ops_install ${MYSQL_LOGDIR}/slow/slow.log && \
# rm -rf /opt/soft/mysql/mysql-cluster-8.0.18-rc-linux-glibc2.12-x86_64.tar.gz && \
rm -rf ${MYSQL_BASEDIR}/mysql-cluster-8.0.18-rc-linux-glibc2.12-x86_64


# 查看mysql安装文件
ls -al ${MYSQL_BASEDIR}
# 解压后，可以看到当前目录下，如下文件


# 打开防火墙端口
firewall-cmd --zone=public --add-port=13306/tcp --permanent && \
firewall-cmd --zone=public --add-port=33060/tcp --permanent && \
firewall-cmd --reload && \
firewall-cmd --zone=public --list-ports


# =============================================================================================================


# step 11: 初始化mysql数据库
# 备注，必须加 --lower-case-table-names=1，否则后面启动mysql会提示报错:Different lower_case_table_names settings for server ('1') and data dictionary ('0')
mysqld --initialize --user=mysql --basedir=${MYSQL_BASEDIR} --datadir=${MYSQL_DATADIR} --lower-case-table-names=1
## 或 mysqld --initialize --user=mysql --basedir=/opt/install/mysql --datadir=/opt/data/mysql --lower-case-table-names=1
## 密码 = root@localhost: Q*JU2JrtgPYA
## 执行结果，输出如下：
# [root@CNT7XAPLOD01 ~]# mysqld --initialize --user=mysql --basedir=${MYSQL_BASEDIR} --datadir=${MYSQL_DATADIR} --lower-case-table-names=1
# 2020-07-08T10:39:57.045888Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
# 2020-07-08T10:39:57.045994Z 0 [System] [MY-013169] [Server] /app/mysql/mysql-8.0.18/bin/mysqld (mysqld 8.0.18-cluster) initializing of server in progress as process 1488
# 2020-07-08T10:40:00.761768Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: Q*JU2JrtgPYA
# [root@CNT7XAPLOD01 ~]#

# 创建mysql文件sock的软连接
ln -s /tmp/mysql.sock  ${MYSQL_BASEDIR}/mysql.sock

# ==================================================================================================================================

# step 12: 启动mysql

# 启动mysql
# 命令行，启动mysql数据库
${MYSQL_BASEDIR}/bin/mysqld_safe --defaults-file=${MYSQL_CONF}/mysql.conf --user=mysql
# 或
# /app/mysql/mysql-8.0.18/bin/mysqld_safe  --defaults-file=/app/mysql/mysql-8.0.18/conf/mysql.conf  --user=mysql


# 或 启动时候，console不打印启动日志
# ${MYSQL_BASEDIR}/bin/mysqld_safe --defaults-file=${MYSQL_CONF}/mysql.conf --user=mysql
##  或
# /app/mysql/mysql-8.0.18/bin/mysqld_safe  --defaults-file=/app/mysql/mysql-8.0.18/conf/mysql.conf  --user=mysql


# 停止mysql
# TODO


# 检查mysql: 
# 方法1
ps -ef | grep mysql
# 方法2：
netstat -nltp    

 
# ====================================================================================================================================================

# step 13: 设置开机启动mysql

# 创建mysql.service文件，如下

# 切换到root账户
su root

# 创建mysql.service文件，如下
cat > /etc/systemd/system/mysql.service <<EOF
[Unit]
Description=mysql service
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
User=root
# Type=notify / forking
Type=notify
# TimeoutSec=0让开机启动不处理service启动超时，保证service耗时过长时不会被系统terminating
TimeoutSec=0
ExecStart=${MYSQL_BASEDIR}/bin/mysqld_safe --defaults-file=${MYSQL_CONF}/mysql.conf --user=mysql
RestartSec=5
LimitNOFILE=1000000

[Install]
WantedBy=multi-user.target
EOF


# 注册和启动mysql.service服务
systemctl enable mysql
# 后台启动mysql
systemctl start mysql &
systemctl status mysql
# 检查mysql
ps -ef | grep mysql
# 检查mysql
netstat -nltp | grep mysql 

# ====================================================================================================================================================
